home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Blender 2.49b / blender-2.49b-windows.exe / $_4_ / .blender / scripts / object_drop.py < prev    next >
Text File  |  2009-08-31  |  8KB  |  254 lines

  1. #!BPY
  2. """
  3. Name: 'Drop Onto Ground'
  4. Blender: 249
  5. Group: 'Object'
  6. Tooltip: 'Drop the selected objects onto "ground" objects'
  7. """
  8. __author__= "Campbell Barton"
  9. __url__= ["blender.org", "blenderartists.org"]
  10. __version__= "1.1"
  11.  
  12. __bpydoc__= """
  13. """
  14.  
  15. # --------------------------------------------------------------------------
  16. # Drop Objects v1.0 by Campbell Barton (AKA Ideasman42)
  17. # --------------------------------------------------------------------------
  18. # ***** BEGIN GPL LICENSE BLOCK *****
  19. #
  20. # This program is free software; you can redistribute it and/or
  21. # modify it under the terms of the GNU General Public License
  22. # as published by the Free Software Foundation; either version 2
  23. # of the License, or (at your option) any later version.
  24. #
  25. # This program is distributed in the hope that it will be useful,
  26. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28. # GNU General Public License for more details.
  29. #
  30. # You should have received a copy of the GNU General Public License
  31. # along with this program; if not, write to the Free Software Foundation,
  32. # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  33. #
  34. # ***** END GPL LICENCE BLOCK *****
  35. # --------------------------------------------------------------------------
  36.  
  37.  
  38. from Blender import Draw, Geometry, Mathutils, Window
  39. from Blender.Mathutils import Vector, AngleBetweenVecs, RotationMatrix
  40. import bpy
  41.  
  42.  
  43. GLOBALS = {}
  44. GLOBALS['GROUND_SOURCE'] = [Draw.Create(1), Draw.Create(0)]
  45. GLOBALS['GROUND_GROUP_NAME'] = Draw.Create('terrain')
  46. GLOBALS['DROP_AXIS'] = [Draw.Create(1), Draw.Create(0)]                        # on what axis will we drop?
  47. GLOBALS['DROP_OVERLAP_CHECK'] = Draw.Create(1)                # is the terrain a single skin?
  48. GLOBALS['DROP_ORIENT'] = Draw.Create(1)
  49. GLOBALS['DROP_ORIENT_VALUE'] = Draw.Create(100.0)
  50. GLOBALS['EVENT'] = 2
  51. GLOBALS['MOUSE'] = None
  52.  
  53. def collect_terrain_triangles(obs_terrain):
  54.     terrain_tris = []
  55.     me = bpy.data.meshes.new()
  56.     
  57.     for ob in obs_terrain:
  58.         def blend_face_to_terrain_tris(f):
  59.             no = f.no
  60.             cos = [v.co for v in f]
  61.             if len(cos) == 4:    return [(cos[0], cos[1], cos[2], no), (cos[0], cos[2], cos[3], no)]
  62.             else:                return [(cos[0], cos[1], cos[2], no), ]
  63.         
  64.         # Clear
  65.         me.verts = None
  66.         try:    me.getFromObject(ob)
  67.         except:    pass
  68.         
  69.         me.transform(ob.matrixWorld)
  70.         for f in me.faces: # may be [], thats ok
  71.             terrain_tris.extend( blend_face_to_terrain_tris(f) )
  72.     
  73.     me.verts = None # clear to save ram
  74.     return terrain_tris
  75.  
  76. def calc_drop_loc(ob, terrain_tris, axis):
  77.     pt = Vector(ob.loc)
  78.     
  79.     isect = None
  80.     isect_best = None
  81.     isect_best_no = None
  82.     isect_best_len = 0.0
  83.     
  84.     for t1,t2,t3, no in terrain_tris:
  85.         #if Geometry.PointInTriangle2D(pt, t1,t2,t3):
  86.         isect = Mathutils.Intersect(t1, t2, t3, axis, pt, 1) # 1==clip
  87.         if isect:
  88.             if not GLOBALS['DROP_OVERLAP_CHECK'].val:
  89.                 # Find the first location
  90.                 return isect, no
  91.             else:
  92.                 if isect_best:
  93.                     isect_len = (pt-isect).length
  94.                     if isect_len < isect_best_len:
  95.                         isect_best_len = isect_len
  96.                         isect_best = isect
  97.                         isect_best_no = no
  98.                     
  99.                 else:
  100.                     isect_best_len = (pt-isect).length
  101.                     isect_best = isect;
  102.                     isect_best_no = no
  103.     
  104.     return isect_best, isect_best_no
  105.  
  106.  
  107. def error_nogroup():
  108.     Draw.PupMenu('The Group name does not exist')
  109. def error_noact():
  110.     Draw.PupMenu('There is no active object')
  111. def error_noground():
  112.     Draw.PupMenu('No triangles could be found to drop the objects onto')
  113. def error_no_obs():
  114.     Draw.PupMenu('No objects selected to drop')
  115.  
  116. # event and value arnt used
  117. def terrain_clamp(event, value):
  118.     
  119.     sce = bpy.data.scenes.active
  120.     if GLOBALS['GROUND_SOURCE'][0].val:
  121.         obs_terrain = [sce.objects.active]
  122.         if not obs_terrain[0]:
  123.             error_noact()
  124.             return
  125.     else:
  126.         try:    obs_terrain = bpy.data.groups[ GLOBALS['GROUND_GROUP_NAME'].val ].objects
  127.         except:
  128.             error_nogroup()
  129.             return
  130.     
  131.     obs_clamp = [ob for ob in sce.objects.context if ob not in obs_terrain and not ob.lib]
  132.     if not obs_clamp:
  133.         error_no_obs()
  134.         return
  135.     
  136.     terrain_tris = collect_terrain_triangles(obs_terrain)
  137.     if not terrain_tris:
  138.         error_noground()
  139.         return
  140.     
  141.     
  142.     
  143.     if GLOBALS['DROP_AXIS'][0].val:
  144.         axis = Vector(0,0,-1)
  145.     else:
  146.         axis = Vector(Window.GetViewVector())
  147.     
  148.     do_orient = GLOBALS['DROP_ORIENT'].val
  149.     do_orient_val = GLOBALS['DROP_ORIENT_VALUE'].val/100.0
  150.     if not do_orient_val: do_orient = False
  151.     
  152.     for ob in obs_clamp:
  153.         loc, no = calc_drop_loc(ob, terrain_tris, axis)
  154.         if loc:
  155.             if do_orient:
  156.                 try:    ang = AngleBetweenVecs(no, axis)
  157.                 except:ang = 0.0
  158.                 if ang > 90.0:
  159.                     no = -no
  160.                     ang = 180.0-ang
  161.                 
  162.                 if ang > 0.0001:
  163.                     ob_matrix = ob.matrixWorld * RotationMatrix(ang * do_orient_val, 4, 'r', axis.cross(no))
  164.                     ob.setMatrix(ob_matrix)
  165.             
  166.             ob.loc = loc
  167.     
  168.     # to make the while loop exist
  169.     GLOBALS['EVENT'] = EVENT_EXIT
  170.  
  171.  
  172. # UI STUFF ------------------------
  173. def do_axis_z(e,v):    
  174.     GLOBALS['DROP_AXIS'][0].val = 1
  175.     GLOBALS['DROP_AXIS'][1].val = 0
  176.     GLOBALS['EVENT'] = e
  177.  
  178. def do_axis_view(e,v):
  179.     GLOBALS['DROP_AXIS'][0].val = 0
  180.     GLOBALS['DROP_AXIS'][1].val = 1
  181.     GLOBALS['EVENT'] = e
  182.  
  183. def do_ground_source_act(e,v):
  184.     GLOBALS['GROUND_SOURCE'][0].val = 1
  185.     GLOBALS['GROUND_SOURCE'][1].val = 0
  186.     GLOBALS['EVENT'] = e
  187.  
  188. def do_ground_source_group(e,v):
  189.     GLOBALS['GROUND_SOURCE'][0].val = 0
  190.     GLOBALS['GROUND_SOURCE'][1].val = 1
  191.     GLOBALS['EVENT'] = e
  192.  
  193. def do_ground_group_name(e,v):
  194.     try: g =    bpy.data.groups[v]
  195.     except: g =    None
  196.     if not g:    error_nogroup()
  197.     GLOBALS['EVENT'] = e
  198.     
  199. def do_dummy(e,v):
  200.     GLOBALS['EVENT'] = e
  201.  
  202. EVENT_NONE = 0
  203. EVENT_EXIT = 1
  204. EVENT_REDRAW = 2
  205. def terain_clamp_ui():
  206.     
  207.     # Only to center the UI
  208.     x,y = GLOBALS['MOUSE']
  209.     x-=40
  210.     y-=70
  211.     
  212.     Draw.Label('Drop Axis', x-70,y+120, 60, 20)
  213.     Draw.BeginAlign()
  214.     GLOBALS['DROP_AXIS'][0] = Draw.Toggle('Z',        EVENT_REDRAW, x+20, y+120, 30, 20, GLOBALS['DROP_AXIS'][0].val, 'Drop down on the global Z axis', do_axis_z)
  215.     GLOBALS['DROP_AXIS'][1] = Draw.Toggle('View Z',    EVENT_REDRAW, x+50, y+120, 70, 20, GLOBALS['DROP_AXIS'][1].val, 'Drop allong the view vector', do_axis_view)
  216.     Draw.EndAlign()
  217.     
  218.     # Source
  219.     Draw.Label('Drop on to...', x-70,y+90, 120, 20)
  220.     Draw.BeginAlign()
  221.     GLOBALS['GROUND_SOURCE'][0] = Draw.Toggle('Active Object',    EVENT_REDRAW, x-70, y+70, 110, 20, GLOBALS['GROUND_SOURCE'][0].val, '', do_ground_source_act)
  222.     GLOBALS['GROUND_SOURCE'][1] = Draw.Toggle('Group',            EVENT_REDRAW, x+40, y+70, 80, 20, GLOBALS['GROUND_SOURCE'][1].val, '', do_ground_source_group)
  223.     if GLOBALS['GROUND_SOURCE'][1].val:
  224.         GLOBALS['GROUND_GROUP_NAME'] = Draw.String('GR:',    EVENT_REDRAW+1001, x-70, y+50, 190, 20, GLOBALS['GROUND_GROUP_NAME'].val, 21, '', do_ground_group_name)
  225.     Draw.EndAlign()
  226.     
  227.     GLOBALS['DROP_OVERLAP_CHECK'] = Draw.Toggle('Overlapping Terrain', EVENT_NONE, x-70, y+20, 190, 20, GLOBALS['DROP_OVERLAP_CHECK'].val, "Check all terrain triangles and use the top most (slow)")
  228.     
  229.     Draw.BeginAlign()
  230.     GLOBALS['DROP_ORIENT'] = Draw.Toggle('Orient Normal', EVENT_REDRAW, x-70, y-10, 110, 20, GLOBALS['DROP_ORIENT'].val, "Rotate objects to the face normal", do_dummy)
  231.     if GLOBALS['DROP_ORIENT'].val:
  232.         GLOBALS['DROP_ORIENT_VALUE'] = Draw.Number('', EVENT_NONE, x+40, y-10, 80, 20, GLOBALS['DROP_ORIENT_VALUE'].val, 0.0, 100.0, "Percentage to orient 0.0 - 100.0")
  233.     Draw.EndAlign()
  234.     
  235.     Draw.PushButton('Drop Objects', EVENT_EXIT, x+20, y-40, 100, 20, 'Drop the selected objects', terrain_clamp)
  236.     
  237.     # So moving the mouse outside the popup exits the while loop
  238.     GLOBALS['EVENT'] = EVENT_EXIT
  239.  
  240. def main():
  241.     
  242.     # This is to set the position if the popup
  243.     GLOBALS['MOUSE'] = Window.GetMouseCoords()
  244.     
  245.     # hack so the toggle buttons redraw. this is not nice at all
  246.     while GLOBALS['EVENT'] == EVENT_REDRAW:
  247.         Draw.UIBlock(terain_clamp_ui, 0)
  248.     
  249. if __name__ == '__main__':
  250.     main()
  251.  
  252. GLOBALS.clear()
  253.  
  254.